/**
 * Copyright 2011 Mathieu Magnaudet
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package edu.enac.geoapp.wims.model;

import java.util.List;
import java.util.StringTokenizer;

import edu.enac.geoapp.wims.exception.WimsException;

/**
 * Utility class for the building of the polygons
 * @author mathieu
 *
 */
public class ProductBuilderUtilities {
	
	/**
	 * Adds points to a list of point
	 * @param pointList the list to be completed
	 * @param points the points to add
	 * @throws WimsException if there is an odd number of elements in the string describing the points
	 */
	public static void addPoint(List<Point> pointList, String points) throws WimsException {
		StringTokenizer tokenizer = new StringTokenizer(points);
		if(tokenizer.countTokens()%2 != 0) {
			throw new WimsException("Odd number of elements");
		}
		while(tokenizer.hasMoreTokens()) {
			float latitude = Float.parseFloat(tokenizer.nextToken());
			float longitude = Float.parseFloat(tokenizer.nextToken());
			Point p = new Point(latitude, longitude);
			
			pointList.add(p);
		}
	}
	
	/**
	 * Adds a hole in a list of points
	 * @param pointList the list to be completed
	 * @param points the points to add
	 * @throws WimsException if there is an odd number of elements in the string describing the points
	 */
	public static void addHole(List<Point> pointList, String points) throws WimsException {
		StringTokenizer tokenizer = new StringTokenizer(points);
		if(tokenizer.countTokens()%2 != 0) {
			throw new WimsException("Odd number of elements");
		}
		while(tokenizer.hasMoreTokens()) {
			float latitude = Float.parseFloat(tokenizer.nextToken());
			float longitude = Float.parseFloat(tokenizer.nextToken());
			Point p = new Point(latitude, longitude);
			
			pointList.add(p);
		}
	}
	
	/**
	 * Builds a {@link Point} from the coordinates contained in a String
	 * @param point the string to decode
	 * @return the point
	 * @throws WimsException
	 */
	public static Point getPoint(String point) throws WimsException {
		StringTokenizer tok = new StringTokenizer(point);
		if (tok.countTokens() != 2)
			throw new WimsException("Illegal number of token");
		float latitude = Float.parseFloat(tok.nextToken());
		float longitude = Float.parseFloat(tok.nextToken());
		return new Point(latitude, longitude);
	}
	
	/**
	 * Retrieve a {@link Trend} from a String
	 * @param value the string to decode
	 * @return the found Trend or null if none is retrieved
	 */
	public static Trend getTrend(String value) {
		if (value.equalsIgnoreCase(Trend.constant.name()))
			return Trend.constant;
		if (value.equalsIgnoreCase(Trend.decaying.name()))
			return Trend.decaying;
		if (value.equalsIgnoreCase(Trend.growing.name()))
			return Trend.growing;
		return null;
	}
	
	/**
	 * Retrieve a {@link LayerType} from a name
	 * @param value the string to decode
	 * @return the found LayerType or null if non is retrieved
	 */
	public static LayerType getLayerType(String value) {
		if (value.equalsIgnoreCase(LayerType.bottom.name()))
			return LayerType.bottom;
		if (value.equalsIgnoreCase(LayerType.top.name()))
			return LayerType.top;
		return null;
	}
	

}
